/**
 * 
 */

package net.messagenet.mazor.web.mngbeans;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.messagenet.mazor.DO.CustomerDO;
import net.messagenet.mazor.ejb.facade.MZRFacadeLocal;
import net.messagenet.mazor.exception.MZRInternalServerException;
import net.messagenet.mazor.infra.cache.UserCacheManager;

import org.ajax4jsf.model.KeepAlive;
import org.apache.log4j.Logger;

/**
 * @author Idan
 *
 */
@KeepAlive
public class CustomerEditManagedBean implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5388230760485952510L;
	private MZRFacadeLocal facade=null;
	

	private Logger logger = Logger.getLogger(this.getClass());
	private CustomerDO customer;
	private CustomerDO customerE;
	private UserCacheManager cacheManager;
	private String hiddenCusomerName;

	@PostConstruct
	public void init() {
		
		InitialContext ic;
			
		try {
				ic = new InitialContext();
				facade = (MZRFacadeLocal) ic.lookup("MZRFacade/local");
			} catch (NamingException e) {
				logger.error(e.getMessage(),e);
			}
			
			setCustomerE(new CustomerDO());
			setCustomer(new CustomerDO());
	   
			try {
				cacheManager = facade.getUserCacheManager();
			} catch (MZRInternalServerException e) {
				logger.error(e.getMessage(), e);
			}

	}
	/**
	 * this method is called by the ajax auto complete.
	 * 
	 * @param suggest
	 * @return
	 */
    public List<CustomerDO> autoCompleteCustomer(Object suggest){
    	List<CustomerDO> result = cacheManager.getCustomersWithPrefix((String)suggest);
    	return result;
    }
	public void setCustomer(CustomerDO customer) {
		this.customer = customer;
	}


	public CustomerDO getCustomer() {
		return customer;
	}
	public void setCustomerE(CustomerDO customerE) {
		this.customerE = customerE;
	}


	public CustomerDO getCustomerE() {
		return customerE;
	}


	public void setHiddenCusomerName(String hiddenCusomerName) {
		this.hiddenCusomerName = hiddenCusomerName;
	}
	public String getHiddenCusomerName() {
		return hiddenCusomerName;
	}
	/**
	 * This method is called by JSF. it creates a new customer
	 * 
	 * @return the address of the next page when successes
	 */
	public String createCustomer(){
		logger.debug("create new POS request");
		try {
			facade.createOrEditCustomer(customer);
			
		}catch (MZRInternalServerException e) {
			logger.error(e.getMessage(), e);
			return "opr/failedopr.jsp";
		} 

		return "opr/successsavedata.jsp";
		
	}
	/**
	 * This method is called by JSF. it updates existing customer
	 * 
	 * @return the address of the next page when successes
	 */
	public String updateCustomer(){
		logger.debug("create new POS request");
		try {
			facade.createOrEditCustomer(customerE);
			
		}catch (MZRInternalServerException e) {
			logger.error(e.getMessage(), e);
			return "opr/failedopr.jsp";
		}
		return "opr/successsavedata.jsp";
		
	}


}
